package com.liziba.demo.slice;

import com.liziba.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {

    /** 文本组件 */
    Text text;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 通过id寻找组件对象
        // 1、找到布局对象
        DirectionalLayout layout = (DirectionalLayout) this.findComponentById(ResourceTable.Id_dl);
        // 2、找到文本对象
        text = (Text) this.findComponentById(ResourceTable.Id_text_helloworld);
        // 3、给整个布局DirectionalLayout添加滑动事件
        layout.setTouchEventListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    float startX;
    float startY;
    float endX;
    float endY;

    /**
     * 滑动事件触发后调用的方法
     *
     * @param component     滑动事件触发的组件 -- 这里是DirectionalLayout
     * @param touchEvent    事件的类型，上面有说到三种按下、滑动、抬起，其实有更多,如下所示
     *
     * @return
     */
    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {

        // 通过id比较可以验证component组件就是DirectionalLayout
        MmiPoint pointerPosition;
        int id = component.getId();
        if (id == ResourceTable.Id_dl) {
            // 操作类型
            int action = touchEvent.getAction();
            if (TouchEvent.PRIMARY_POINT_DOWN == action) {
                // 按下操作
                // getPointerPosition()方法的参数指的是手指的索引，可能存在多个手指同时滑动，比如小米手机三个手指头同时向下滑动截屏
                // 目前这里只有一个手指头滑动，所以传入0
                pointerPosition = touchEvent.getPointerPosition(0);
                startX = pointerPosition.getX();
                startY = pointerPosition.getY();
            } else if (TouchEvent.POINT_MOVE == action) {
                // 滑动操作 过程我们不需要考虑，我们只确定按下时的位置和松开的位置进行比较
//                pointerPosition = touchEvent.getPointerPosition(0);
//                x = pointerPosition.getX();
//                y = pointerPosition.getY();
//                text.setText("滑动："+ x + "--" + y);
            } else if (TouchEvent.PRIMARY_POINT_UP == action) {
                // 松开操作
                pointerPosition = touchEvent.getPointerPosition(0);
                endX = pointerPosition.getX();
                endY = pointerPosition.getY();
                // y轴不变，x变大
                if (Math.abs(endY - startY) <= 50 && endX > startX) {
                    text.setText("右滑");
                } else if (Math.abs(endY - startY) <= 50 && endX < startX) {
                    text.setText("左滑");
                } else if (Math.abs(endX - startX) <= 50 && endY < startY) {
                    text.setText("上滑");
                } else if (Math.abs(endX - startX) <= 50 && endY > startY) {
                    text.setText("下滑");
                }
            }
        }
        // 返回值需要修改为true
        return true;
    }
}
